/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.OpaqueExpression;

import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagram.classdiagram.tabbedproperties.composite.OpaqueExpressionBodiesComposite;

/**
 * 
 */
public class OpaqueExpressionSection extends PropertyViewSection {
	
	/**
	 * 
	 */
	private OpaqueExpressionBodiesComposite opaqueExpressionComposite;
	
	/**
	 * 
	 */
	protected Text editionArea;
	
	/**
	 * 
	 */
	protected SelectionListener tableListener;
	
	/**
	 * 
	 */
	protected FocusListener editionAreaListener;
		
    /* (non-Javadoc)
     * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
     */
    @Override
	public void createControls(Composite parent,
            TabbedPropertySheetPage tabbedPropertySheetPage) {
        super.createControls(parent, tabbedPropertySheetPage);
        // set a grid Layout
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        layout.makeColumnsEqualWidth = true;
        layout.marginWidth = 0; // remove additional spaces
        layout.marginHeight = 0; // remove additional spaces
        parent.setLayout(layout);
        
        GridData data = new GridData();
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = SWT.FILL;
		
		// table for Parameters
		opaqueExpressionComposite = new OpaqueExpressionBodiesComposite();
		opaqueExpressionComposite.createContent(parent, tabbedPropertySheetPage.getWidgetFactory());
		opaqueExpressionComposite.setLayoutData(data);
		
		

		// edition area
		editionArea = getWidgetFactory().createText(parent, "", SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		editionArea.setLayoutData(data);
		
		// Listener for edition
		final Table table = opaqueExpressionComposite.getTable();
		table.addSelectionListener(new SelectionListener() {
			
			public void widgetSelected(SelectionEvent e) {
				int selectionIndex = table.getSelectionIndex();
				if(selectionIndex != -1){ // selection not null
					OpaqueExpression opaqueExpressionElement = (OpaqueExpression)opaqueExpressionComposite.getElement().getUml2Element();
					editionArea.setText((String)(opaqueExpressionElement).getBodies().get(selectionIndex));
				}
			}
			
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		
		
		editionArea.addFocusListener(new FocusListener() {
    		public void focusGained(FocusEvent e){
    		}
    		public void focusLost(FocusEvent e){
    			if((table.getSelection() != null) && (table.getSelection().length > 0)) {
    				int selectionIndex = table.getSelectionIndex();
    				OpaqueExpression opaqueExpressionElement = (OpaqueExpression)opaqueExpressionComposite.getElement().getUml2Element();
    				opaqueExpressionElement.getBodies().set(selectionIndex, editionArea.getText().trim());
    			}
    			refresh();
    		}
        });

		editionArea.setToolTipText("Body edition area");
    }
    

    /*
     * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
     */
    /**
     * 
     */
    @Override
	public void refresh() {
    	opaqueExpressionComposite.refresh();
    	final Table table = opaqueExpressionComposite.getTable();
    	if (!table.isDisposed() && !editionArea.isDisposed()) {
    		int selectionIndex = table.getSelectionIndex();
    		if(selectionIndex != -1){ // selection not null
    			OpaqueExpression opaqueExpressionElement = (OpaqueExpression)opaqueExpressionComposite.getElement().getUml2Element();
    			editionArea.setText((String)(opaqueExpressionElement).getBodies().get(selectionIndex));
    		}
    	}

    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#setInput(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
     */
    @Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
        super.setInput(part, selection);
        Assert.isTrue(selection instanceof IStructuredSelection);
        Object input = ((IStructuredSelection) selection).getFirstElement();
        if (input instanceof IUMLElementEditPart) {
        	org.eclipse.uml2.uml.Element element = ((IUMLElementEditPart) input).getUmlElement();
        	if(element instanceof org.eclipse.uml2.uml.OpaqueExpression) {
        		com.cea.papyrus.umlutils.OpaqueExpression opaqueExpression = new com.cea.papyrus.umlutils.OpaqueExpression((org.eclipse.uml2.uml.OpaqueExpression)element);
        		opaqueExpressionComposite.setElement(opaqueExpression);
        	}
        }
    }
    
    /*
     * (non-Javadoc)
     * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
     */
    public void dispose(){
    	super.dispose();
    	if(editionArea!=null && !editionArea.isDisposed() && editionAreaListener!=null)
    		editionArea.removeFocusListener(editionAreaListener);
    	if(opaqueExpressionComposite!=null && opaqueExpressionComposite.getTable()!=null && !opaqueExpressionComposite.getTable().isDisposed() && tableListener!=null)
    		opaqueExpressionComposite.getTable().removeSelectionListener(tableListener);
    	if(opaqueExpressionComposite!=null)
    		opaqueExpressionComposite.dispose();
    }
}    
